package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Service
public class SpuManageServiceImpl implements SpuManageService {

    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuPosterService spuPosterService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;

    @Autowired
    private BaseSaleAttrService baseSaleAttrService;



    @Override
    public Page<SpuInfo> getSpuListByPage(Page<SpuInfo> iPage, Long category3Id) {
        //1.构建查询查询 排序
        LambdaQueryWrapper<SpuInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuInfo::getCategory3Id, category3Id);
        queryWrapper.orderByDesc(SpuInfo::getUpdateTime);
        //2.执行分页查询
        iPage = spuInfoService.page(iPage, queryWrapper);
        return iPage;
    }

    @Override
    public List<BaseSaleAttr> getBaseSaleAttrList() {
        return baseSaleAttrService.list();
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        //1.将商品基本信息封装SpuInfo对象 存入spu_info表
        spuInfoService.save(spuInfo);
        Long spuId = spuInfo.getId();
        //2.将提交商品图片封装到SpuImage集合中 存入spu_image表
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)) {
            spuImageList.stream().forEach(spuImage -> {
                spuImage.setSpuId(spuId);
            });
            //批量执行保存
            spuImageService.saveBatch(spuImageList);
        }
        //3.将提交海报图片封装到SpuPost集合中 存入spu_poster表
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuPosterList)) {
            spuPosterList.stream().forEach(spuPoster -> {
                spuPoster.setSpuId(spuId);
            });
            spuPosterService.saveBatch(spuPosterList);
        }
        //4.将提交销售属性名称SpuSaleAttr集合 存入spu_sale_attr表
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
            spuSaleAttrList.stream().forEach(spuSaleAttr -> {
                spuSaleAttr.setSpuId(spuId);
                //5.将提交销售属性值SpuSaleAttrValue集合 存入spu_sale_attr_value表
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)) {
                    spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
                        //关联商品SPUID
                        spuSaleAttrValue.setSpuId(spuId);
                        //为平台属性名称赋值
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                    });
                    spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
                }
            });
            spuSaleAttrService.saveBatch(spuSaleAttrList);
        }
    }
}
